<?php

class SiteController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	 public $layout='//layouts/column1';

	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$featured_post = Post::model()->findAll(array("condition"=>'featured=1', "order"=>'created DESC',"limit"=>15));
		$this->render('index', array('featured_post' => $featured_post));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->name}<br>".
					"Reply-To: {$model->email}<br>".
					"Content: <br>"."{$model->body}";
            	$message = new YiiMailMessage;
				$message->setBody($headers, 'text/html');
				$message->subject = $model->subject;
				// Send mail cho một địa chỉ mail
				$message->addTo('ngoduytrung2901@gmail.com');
				//Địa chỉ mủa mail gửi đi và tên muốn đặt.
				$message->setFrom($model->email, $model->name);
				//Địa chỉ mail nhận trả lời của những mail đã giử tới
				$message->setReplyTo($model->email);

				Yii::app()->mail->send($message);
 				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
 				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}
}